#include "Http.hpp"
#include "Common.hpp"
#include <memory>
#include <unistd.h>
#include <signal.h>

int main(int argc, char *args[])
{
    if (argc != 2)
    {
        LOG(LogLevel::FATAL) << "Usage: " << args[0] << " port";
        exit(USAGE_ERROR);
    }

    // USE_FILE_STRATEGY();

    Http http;
    http.RegisterRedirect("/qq", "https://qq.com");
    http.RegisterMicroService("/login", [](const HttpRequest &req, HttpResponse &res){
        // 仅作测试，不管req的参数了
        res.SetStatus(302);
        res.AddHeader("Location", "/login-success.html"); 
    });

    in_port_t port = std::stoi(args[1]);
    TCPServer server(port, [&http](const std::shared_ptr<TCPConnectSocket> &con_socket){ 
        http.RequestHandler(con_socket); 
    });

    // daemon(0, 0);
    signal(SIGCHLD, SIG_IGN);
    server.Run();
    return 0;
}
